#!/bin/sh

##脚本名称
SHELL_NAME=${0##*/}
## 服务名称
SERVICE_NAME=icp-apollo-admin
## 启动Jar包名称  
JAR_NAME=${SERVICE_NAME}.jar
#PID  代表是PID文件
PID=${SERVICE_NAME}.pid
## 服务ID
APP_ID=${SERVICE_NAME}
## 日志路径
LOG_DIR=/tmp/logs/${SERVICE_NAME}
## 服务器IP
SERVER_IP=
if  [[ ! -n "${SERVER_IP}" ]] ;then
    SERVER_IP=localhost
fi
## 服务端口
SERVER_PORT=8090
## 健康检查地址
SERVER_CHECK_URL=http://${SERVER_IP}:${SERVER_PORT}/health

echo "健康检查地址:"${SERVER_CHECK_URL}
## Adjust memory settings if necessary
export JAVA_OPTS="-Xms2560m -Xmx2560m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=1536m -XX:MaxNewSize=1536m -XX:SurvivorRatio=8"

## Only uncomment the following when you are using server jvm
#export JAVA_OPTS="$JAVA_OPTS -server -XX:-ReduceInitialCardMarks"

########### The following is the same for configservice, adminservice, portal ###########
export JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSPermGenSweepingEnabled -XX:+ExplicitGCInvokesConcurrent -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"
export JAVA_OPTS="$JAVA_OPTS -Dserver.port=$SERVER_PORT  -Xloggc:$LOG_DIR/gc.log -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M -XX:HeapDumpPath=$LOG_DIR/HeapDumpOnOutOfMemoryError/"
export JAVA_OPTS="$JAVA_OPTS -Dapp.id=$APP_ID -Denv=$2"

#使用说明，用来提示输入参数
usage() {
    echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
    exit 1
}

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}' `
  #如果不存在返回1，存在返回0     
  if [[ -z "${pid}" ]]; then
    return 1
  else
    return 0
  fi
}

#启动方法
start(){
  is_exist
  if [[ $? -eq "0" ]]; then
    echo ">>> ${JAR_NAME} 服务正在运行,进程ID为 ${pid} 请使用 sh ${SHELL_NAME} stop 命令来停止服务,或使用 sh ${SHELL_NAME} restart 命令来重启服务 <<<"
  else
    cd `dirname $0`/..
    nohup java -jar ${JAR_NAME} >/dev/null 2>&1 &
    echo $! > $PID
    echo ">>>  ${JAR_NAME} 服务正在启动! PID=$! <<<"
   fi
  }

#停止方法
stop(){
  pidf=$(cat $PID)
  echo ">>> 开始停止 进程ID为 ${pidf} 的服务 <<<"
  kill ${pidf}
  rm -rf ${PID}
  sleep 2
  is_exist
  if [[ $? -eq "0" ]]; then
    echo ">>>停止进程失败,强制杀死该进程 <<<"
    kill -9  $pid
    sleep 2
    echo ">>> 进程已被杀死 <<<"
  else
    echo ">>> 服务未运行 <<<"
  fi  
}

#输出运行状态
status(){
  is_exist
  if [[ $? -eq "0" ]]; then
    echo ">>> 服务正在运行,进程ID为 ${pid} <<<"
  else
    echo ">>> 进程未运行 <<<"
  fi
}

#重启
restart(){
  stop
  start
}

#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac
exit 0